#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>
#include<conio.h>
#include <fstream>
#include <sys/select.h>
using namespace std;

	bool CheckPassWord(string password)/*以"*"输入密码，三次机会*/
{
    bool judge = false;
    int i = 0;
    
    int times = 1;
    string s;
    char ch;
    char pw[50];
    while(times <= 3)
    {
        i = 0;
        while((ch = _getch()) != '\n')
        {
            if(ch == '\b' && i > 0)//退格
            {
                printf("\b \b");
                i--;
            }
            else
            {
                pw[i++] = ch;
                cout << '*';
            }
        }
        pw[i] = '\0';
        cout << endl;
        if(password.compare(pw) != 0)
        {
            if(times == 3)
                cout << "密码错误，退出！" << endl << endl;
            else
                cout << "密码错误，剩余" << (3-times) << "次机会：\n";
            times++;
        }
        else
        {
            cout << "密码正确！"<< endl;
            judge=true;
            return judge;
        }
    }
    if(times == 4)
        return judge;
    return judge;
}
	

static void sleep_ms(unsigned int secs)

{

    struct timeval tval;

    tval.tv_sec=secs/1000;

    tval.tv_usec=(secs*1000)%1000000;

    select(0,NULL,NULL,NULL,&tval);

}

void delay(float second)//延迟功能
{
time_t start_time, cur_time;//定义时间变量
time(&start_time); //获取time_t类型的开始时
//获取time_t类型的时间，结束时间减去开始时间小于给定的时间则退出循环
do {time(&cur_time);} 
while ((cur_time - start_time) < second);
}


int main()
{
	

	
	
	srand((unsigned int)time(NULL));
	int w,n,s=0;
	int t;
	int xu=1;
	int ss=0;
	int suan=101,zong;
int sum=0,sum0=0,sum1=0,sum2=0;
	int sum3=0,sum4=0,sum5=0,sum6=0;
	int sum7=0,sum8=0,sum9=0,sum10=0;
	int one,two;
	int cin1,cin2;
	float ji,ji1;
int su;
char ch5;
/*cout<<"要计算几次？\n";
cin>>cin2;
cout<<"\r次"<<endl;
loop:for(int xun=0;xun<cin2;xun++){*/
	string ch4="1";
	string password = ch4; //原始密码
    cout << "输入密码：";
    if(CheckPassWord(password)!=true);
	cout<<"先计算再抽奖(100以内的加法）\n"<<endl;		
	one=rand()%suan;
	two=rand()%suan;
	zong=one+two;
	cin1=zong;
	cout<<one<<"+"<<two<<"="<<cin1<<endl;
	if(cin1==zong){
		cout<<"\n正确！\n"<<endl; 
		cout<<"设置输出速度毫秒\n(1s=1000ms，0直接输出)："<<endl;
		cin>>su;
		cout<<"\n"<<endl;
		cout<<"请输入有多少个东西\n(实际上只有11个能抽到东西，以上则是抽不中居多)："<<endl;
		cin>>t;
		ji=(float)11/t;
		cout<<"预计抽奖命中几率为："<<ji*100<<"%"<<endl;
	cout<<"请输入要抽奖的次数(提示：若抽中3次炸弹游戏结束)：";
	cin>>n;
  
    	cout<<"====开始抽"<<n<<"次奖====\n"<<endl;
    for(int x=0;x<n;x++)
    {
    sleep_ms(su);//睡眠
    	w=rand()%t;
    	switch(w)
    	{
    		case 0:
    		cout<<xu<<".";
    	cout<<"得到10美元"<<sum9+1<<"次"<<endl;
    	xu+=1;
    	sum1+=10;
    	sum9+=1;
    	break;
    		case 1:
    			cout<<xu<<".";
    	cout<<"恭喜你获得雪人"<<sum+1<<"次"<<endl;
    	xu+=1;
    	sum+=1;
    	break;
    	case 2:
    		cout<<xu<<".";
    	cout<<"恭喜你获得车子"<<sum0+1<<"次"<<endl;
    	xu+=1;
    	sum0+=1;
    	break;
    	case 3:
    			cout<<xu<<".";
    	cout<<"恭喜你获得礼物"<<sum3+1<<"次"<<endl;
    		xu+=1;
    		sum3+=1;
    	break;
    	case 4:
    			cout<<xu<<".";
    	cout<<"恭喜你获得钻戒"<<sum4+1<<"次"<<endl;
    		xu+=1;
    		sum4+=1;
    	break;
    	case 5:
    			cout<<xu<<".";
    	cout<<"恭喜你获得蛋糕"<<sum5+1<<"次"<<endl;
    		xu+=1;
    		sum5+=1;
    	break;
    	case 6:
    			cout<<xu<<".";
    	cout<<"恭喜你获得足球"<<sum6+1<<"次"<<endl;
    		xu+=1;
    		sum6+=1;
    	break;
    	case 7:
    			cout<<xu<<".";
    	cout<<"获得炸弹，现在有"<<sum7+1<<"个炸弹"<<endl;
    		xu+=1;
    		sum7+=1;
    	break;
    	case 8:
    			cout<<xu<<".";
    	cout<<"恭喜你获得电脑"<<sum2+1<<"次"<<endl;
    		xu+=1;
    	sum2+=1;
    	break;
    	 case 9:
    	 		cout<<xu<<".";
    	cout<<"得到100美元"<<sum10+1<<"次"<<endl;
    		xu+=1;
    	sum1+=100;
    	sum10+=1;
    	break;
    	case 10:
    	cout<<xu<<".";
    	cout<<"拆一个炸弹，还有"<<sum7-1<<"个炸弹"<<endl;
    	xu+=1;
    	sum7-=1;
    	break;
	default:
    			cout<<xu<<".";
    	cout<<"好遗憾，什么都没中"<<endl;
    			xu+=1;
    			sum8+=1;
    		}
    		   if(sum7==3)
        {
        	cout<<"\n炸弹炸了，抽奖提前结束且不记录"<<endl;
        	
	 if(sum==0){}
        else{
        cout<<"  雪人 \t   共有"<<sum<<"个"<<endl;
        }
        if(sum0==0){}
        else{
         cout<<"  车子  \t   共有"<<sum0<<"辆"<<endl;
        }
        if(sum1==0){}
        else{
          cout<<"  美元 \t  获得"<<sum1<<"美元"<<endl;
        }
        if(sum2==0){}
        else{
           cout<<"  电脑 \t   共有"<<sum2<<"台"<<endl;
        }
        if(sum3==0){}
        else{
           cout<<"  礼物 \t   共有"<<sum3<<"个"<<endl;
        }
        if(sum4==0){}
        else{
           cout<<"  钻戒 \t   共有"<<sum4<<"个"<<endl;
        }
        if(sum5==0){}
        else{
           cout<<"  蛋糕 \t   共有"<<sum5<<"个"<<endl;
        }
        if(sum6==0){}
        else{
           cout<<"  足球 \t   共有"<<sum6<<"个"<<endl;
        }
        if(sum8==0){}
        else{
           cout<<" \n\n\t\t未抽中"<<sum8<<"次";
            cout<<"\n\t抽中"<<n-sum8<<"次";
        }
        ji1=(float)sum8/n;
	cout<<"\n实际抽中几率为"<<100-(ji1*100)<<"%\n";
        	exit(0);
        	}
        else{}

    }
        cout<<"\n抽奖结束，记录文件！\n"<<endl;
        cout<<"统计：\n"<<endl;
       
	 if(sum==0){}
        else{
        cout<<"  雪人 \t   共有"<<sum<<"个"<<endl;
        }
        if(sum0==0){}
        else{
         cout<<"  车子 \t  共有"<<sum0<<"辆"<<endl;
        }
        if(sum1==0){}
        else{
          cout<<"  美元 \t  获得"<<sum1<<"美元"<<endl;
        }
        if(sum2==0){}
        else{
           cout<<"  电脑 \t   共有"<<sum2<<"台"<<endl;
        }
        if(sum3==0){}
        else{
           cout<<"  礼物 \t   共有"<<sum3<<"个"<<endl;
        }
        if(sum4==0){}
        else{
           cout<<"  钻戒  \t   共有"<<sum4<<"个"<<endl;
        }
        if(sum5==0){}
        else{
           cout<<"  蛋糕 \t   共有"<<sum5<<"个"<<endl;
        }
        if(sum6==0){}
        else{
           cout<<"  足球 \t   共有"<<sum6<<"个"<<endl;
        }
        if(sum7==3)
        {
        	cout<<"炸弹炸了，抽奖提前结束且不记录"<<endl;
        	}
        else{}
        if(sum8==0){}
        else{
           cout<<" \n\n\t未抽中"<<sum8<<"次";
           cout<<"\n\t抽中"<<n-sum8<<"次";
        }
           ji1=(float)sum8/n;
	cout<<"\n实际抽中几率为"<<100-(ji1*100)<<"%\n";
        ofstream out("统计结果.txt");
        out<<"统计：";
        out<<"\n雪人\t  "<<sum<<"个";
            out<<"\n车子\t  "<<sum0<<"辆";
                out<<"\n美元\t  "<<sum1;
                    out<<"\n电脑\t  "<<sum2<<"台";
                        out<<"\n礼物\t  "<<sum3<<"个";
                            out<<"\n钻戒\t  "<<sum4<<"个";
                                out<<"\n蛋糕\t  "<<sum5<<"个";
                                    out<<"\n足球\t  "<<sum6<<"个";
                                        out<<"\n炸弹\t  "<<sum7<<"个\n";
                                          out<<"\n未抽中\t  "<<sum8<<"次\n";
                                           out<<"\n\n\t抽中"<<n-sum8<<"次\n";
                                          out<<"预计抽中几率为："<<ji*100<<"%\n"<<endl;
                                          out<<"\n实际抽中几率为："<<100-(ji1*100)<<"%\n\n";
                                     out<<"你的算数题是："<<one<<"+"<<two<<"="<<cin1<<"\n"<<endl;
                                        
	out.close();
	cin.get();
//	cout<<"\n=========第二次计算=========\n"<<endl;
}
//}		
else{cout<<"错误！"<<endl;
exit(0);
} 
return 0;
}